package com.yangchuang.openpicture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangchuang.openpicture.constant.UserFavoritePictureRedisConstants;
import com.yangchuang.openpicture.mapper.UserFavoriteFolderMapper;
import com.yangchuang.openpicture.pojo.UserFavoriteFolder;
import com.yangchuang.openpicture.service.UserFavoriteFolderService;
import com.yangchuang.openpicture.util.RedisUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserFavoriteFolderServiceImpl extends ServiceImpl<UserFavoriteFolderMapper, UserFavoriteFolder> implements UserFavoriteFolderService {
    private final RedisUtils redisUtils;
    private final UserFavoriteFolderMapper userFavoriteFolderMapper;

    @Override
    public UserFavoriteFolder createFolder(Long userId, String name) {
        return null;
    }

    @Override
    public List<UserFavoriteFolder> getUserFolders(Long userId) {
        return null;
    }

    @Override
    public void deleteFolder(Long userId, Long folderId) {
    }

    @Override
    public String getFolderNameById(Long folderId) {
        LambdaQueryWrapper<UserFavoriteFolder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserFavoriteFolder::getId, folderId);
        UserFavoriteFolder folder = this.getOne(wrapper);
        return folder.getName();
    }

    @Override
    public Long getFolderIdByUserIdAndFolderName(String folderName, Long userId) {
        return null;
    }

    @Override
    public Long getFolderIdByUserIdAndFolderName(Long folderId, Long userId) {
        return null;
    }

    @Override
    public String FolderNameAndIdUpdate(Long folderId) {
        //1.先去redis里面查 这里的key能够区分到某个具体的文件夹即可
        String key = UserFavoritePictureRedisConstants.FOLDER_ID_NAME_STRING + folderId;
        String name = (String) redisUtils.stringOps().getValue(key);
        if (name == null) {
            String folderName = this.getFolderNameById(folderId);
            if (folderName != null) {
                //存入redis里面
                redisUtils.stringOps().setValueAndExpire(key, folderName, UserFavoritePictureRedisConstants.FOLDER_ID_NAME_TTL_SECONDS, TimeUnit.SECONDS);
            }
            return folderName;
        }
        return name;
    }
}
